package cn.cqautotest.micalendar.utils;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.CalendarContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresPermission;

import java.util.Calendar;
import java.util.TimeZone;

public class CalendarEventUtils {

    private static final String TAG = CalendarEventUtils.class.getSimpleName();
    private static boolean isInit = false;
    private static ContentResolver sCr;

    public static void init(Context context) {
        if (isInit) {
            throw new IllegalStateException("CalendarEventUtils 已经初始化过了，请勿重复初始化！");
        }
        sCr = context.getContentResolver();
        isInit = true;
    }

    private static void checkIsInitialized() {
        if (!isInit) {
            throw new IllegalStateException("请先调用 init 方法初始化 CalendarEventUtils ！");
        }
    }

    /**
     *
     * @param startCalendar 开始的日历对象
     * @param endCalendar 结束的日历对象
     * @param eventTitle 事件的标题
     * @param eventDescription 事件的描述信息
     * @return
     */
    public static Uri insertEvent(Calendar startCalendar, Calendar endCalendar, String eventTitle, String eventDescription) {
        checkIsInitialized();
        //时间创建
        //Calendar beginTime = Calendar.getInstance();
        //Month value is 0-based. e.g., 0 for January.
        long startMillis = startCalendar.getTimeInMillis();
        long endMillis = endCalendar.getTimeInMillis();
        Log.d(TAG, "beginTime ====== > " + startMillis);
        Log.d(TAG, "endTime ====== > " + endMillis);
        //准备好插入事件数据库的内容
        ContentValues values = new ContentValues();
        //开始时间
        values.put(CalendarContract.Events.DTSTART, startMillis);
        //结束时间
        values.put(CalendarContract.Events.DTEND, endMillis);
        //事件标题
        values.put(CalendarContract.Events.TITLE, eventTitle);
        //事件描述
        values.put(CalendarContract.Events.DESCRIPTION, eventDescription);
        //日历ID，填写其它的会崩溃，不信你试试~
        values.put(CalendarContract.Events.CALENDAR_ID, "1");
        //时间时区
        String timeZone = TimeZone.getDefault().getID();
        Log.d(TAG, "time zone ====== > " + timeZone);
        values.put(CalendarContract.Events.EVENT_TIMEZONE, timeZone);
        Uri uri = sCr.insert(CalendarContract.Events.CONTENT_URI, values);
        Log.d(TAG, "insert result ====== > " + uri);
        return uri;
    }

    public static int delete(@RequiresPermission.Write @NonNull Uri uri, @Nullable String where,
                                  @Nullable String[] selectionArgs) {
        return sCr.delete(uri, where, selectionArgs);
    }

    public final @Nullable
    Cursor query(@RequiresPermission.Read @NonNull Uri uri,
                 @Nullable String[] projection, @Nullable String selection,
                 @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        return sCr.query(uri, projection, selection, selectionArgs, sortOrder, null);
    }
}
